package com.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import com.core.visaInfo.dao.VisaInfoDao;
import com.core.visitor.dao.VisitorDao;
import com.core.visitor.dto.VisitorSearchCondition;
import com.core.visitor.pojo.Visitor;
@Service
public class ExceInputUtil {
	@Resource
	private VisitorDao visitorDao;
	@Resource
	private VisaInfoDao visaInfoDao;
	@SuppressWarnings({ "unused", "static-access" })
	public int  inputExcelReturn(String road) {
//		获取该签证的游客
		String fielName =road.substring(road.lastIndexOf(File.separator)+1);
		Long visaIdLong =Long.parseLong(fielName.substring(0,fielName.indexOf(".")));
		System.out.println(visaIdLong);
		VisitorSearchCondition sc =new VisitorSearchCondition();
		sc.setVisaInfoId(visaIdLong);
		List<Visitor> visitorList = visitorDao.getVisitorList2(sc);
		List<Visitor> updateVisitorList =visitorList;
		Visitor firstPerson =new Visitor();
		List<List<String>> sheetData = new ArrayList<List<String>>();
		int flag =0;
//		读取excel表格并初始化对象
		try {
			InputStream is  = new FileInputStream(road);
			HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);//整个Ecel
			HSSFSheet hssfSheet =hssfWorkbook.getSheetAt(0);//第一页
			int minRowIx = hssfSheet.getFirstRowNum();
			int maxRowIx = hssfSheet.getLastRowNum();
			
//			处理当前页
			for(int rowNum=minRowIx+1;rowNum<=maxRowIx;rowNum++){
//				初始化行 第二行开始忽略表头
				HSSFRow hssfRow = hssfSheet.getRow(rowNum);
				int minColIx = hssfRow.getFirstCellNum();
				int maxColIx = hssfRow.getLastCellNum();
				List<String>rowList =new ArrayList<String>();
				if (hssfRow==null) {
					continue;
				}
				for(int colNum=minColIx;colNum<maxColIx;colNum++){
					HSSFCell cell = hssfRow.getCell(colNum);
					if(cell==null){
						continue;
					}
					cell.setCellType(cell.CELL_TYPE_STRING);
					if(colNum==4 && cell.getStringCellValue()!=null&&cell.getStringCellValue()!=""){
						System.out.println("这是日期格式!");
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
						double value = Double.parseDouble(cell.getStringCellValue());
						Date date = org.apache.poi.ss.usermodel.DateUtil  
		                        .getJavaDate(value);
						String result = sdf.format(date);  
						System.out.println(result);
						rowList.add(result);
						continue;
				}
					if(cell.getStringCellValue()==" "||cell.getStringCellValue()==""){
						continue;
					}
					
					rowList.add(cell.getStringCellValue());
					System.out.println(cell.getStringCellValue());
				}
				if(!rowList.isEmpty()){
					sheetData.add(rowList);
				}
				
			} 
		} catch (FileNotFoundException e) {
			return Consts.ERR_FILE_NOT_FOUND;
			
		}catch (IOException e) {
			// TODO Auto-generated catch block
			return Consts.ERR_FILE_IO;
		}catch (NumberFormatException e) {
			return Consts.ERR_FILE_IO;
		}
//		找出当前的一号人
		for (Visitor visitor : updateVisitorList) {
			if("1".equals(visitor.getIsLead())){
				firstPerson=visitor;
			}
		}
		if (firstPerson==null) {
			return Consts.ERR_FIRSTPERSON_NOT_FOUND;
		}
//		判断数据里是否有一号人
		for (List<String> list : sheetData) {
			for (String stringItem : list) {
				if(firstPerson.getName().equals(stringItem)){
					flag=1;
				}
			}
		}
//		如果有
		if (flag==1) {
			
			for (List<String> list : sheetData) {
				Visitor tempVisitor = new Visitor();
				int flag2 =0;
//				将他变成对象
				tempVisitor.setName(list.get(0));
				tempVisitor.setPinyinName(list.get(1));
				int tempSex=(int)Double.parseDouble(list.get(2));
				tempVisitor.setSex(String.valueOf(tempSex));
				tempVisitor.setPermanentCity(list.get(3));
				tempVisitor.setBirthday(list.get(4));
				tempVisitor.setPassport(list.get(5));
				if(list.size()>6){
				tempVisitor.setComment(list.get(6));	
				}
				for (Visitor visit : updateVisitorList) {
//					如果一样就将相应的更新掉
					if (tempVisitor.getName().equals(visit.getName())) {
						
						visit.setSex(tempVisitor.getSex());
						visit.setPermanentCity(tempVisitor.getPermanentCity());
						visit.setBirthday(tempVisitor.getBirthday());
						visit.setPassport(tempVisitor.getPassport());
						if(tempVisitor.getComment()!=null){
							visit.setComment(tempVisitor.getComment());
						}
//						将flag置为1
						flag2=1;
					}
				}
				if (flag2==1) {
					continue;
				}
//				有多的就加
				tempVisitor.setVisaInfoId(firstPerson.getVisaInfoId());
				tempVisitor.setIsLead("0");
				updateVisitorList.add(tempVisitor);
			}
//			加完后将旧数据删除 再添加
			for (Visitor visitor3 : visitorList) {
				visitorDao.del(visitor3);
			}
			for (Visitor visitor4 : updateVisitorList) {
				visitorDao.add(visitor4);
			}
		}
		else {
			return Consts.ERR_FIRSTPERSON_NOT_MATCH;
		}
		return Consts.SUCCESS;
				
	}
	public static void main(String[] args) {
		Date nowDate =new Date();
		String ss="1993/4/5";
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/M/d");
		String oneString = formatter.format(nowDate);
		System.out.println(oneString+"one");
		try {
			Date twoString = formatter.parse(ss);
			System.out.println(twoString+"two");
			String threeString = formatter.format(twoString);
			System.out.println(threeString+"three");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
